VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsSFX"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'====================================================================================
'clsSFX - Play WAV Files for sound effects
'====================================================================================
'This class loads a sound effect, keeps it on standby and plays it when requested.
'I could have used the PlaySound API (which supports looping), but PlaySound does
'not allow you to play two sound effects at the same time!
'
'Instead this class uses the mciSendString API to play WAV files, crucially also
'allowing more than one sound effect to be heard at a time. The down side is that
'mciSendString cannot loop WAV files automatically!
'
'To get around this we simply make looping irrelevant XD
'The .Play function first checks if the sound is already playing and (by default)
'won't play it again. This way if we call the function over and over again, it won't
'matter. Since the game has a reliable animation loop for each frame, we can use
'this to continue sounds as needed.
'
'For example, if I try play the walking sound every frame, it won't spaz out, but
'rather ignore trying to play it again, until it has finished. Plus this way there's
'no need to stop a looping sound specifically, you just don't call it when you no
'longer need to loop anymore; and that's much easier to code for supprisingly!
Option Explicit

Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Alias As String  'The reference name for MCI strings

'====================================================================================
'Load: Preload a WAV file, it will not begin playing immediately
'====================================================================================
Public Sub Load(ByVal FilePath As String)
    'If a WAV file is already loaded, free that up first
    If Alias <> "" Then Unload Me
    
    Dim SplitString() As String
    SplitString = Split(FilePath, "\")        'Split by directories
    Alias = SplitString(UBound(SplitString))  'Take the last - the filename
    Alias = Left(Alias, Len(Alias) - 4)       'Remove the '.wav'
    
    'Send an MCI command to prepare the device for playing the WAV file
    mciSendString "open " & Chr(34) & FilePath & Chr(34) & " type waveaudio alias " & Alias & " wait", 0&, 0, 0
End Sub

'====================================================================================
'Play: Play the sound
'====================================================================================
Public Sub Play(Optional IgnoreIfPlaying As Boolean = True)
    Dim Msg As String * 255
    mciSendString "status " & Alias & " mode", Msg, 255, 0
    If Left(Msg, 7) = "stopped" Or IgnoreIfPlaying = False Then
        'Play from the beginning
        mciSendString "play " & Alias & " from 0", 0&, 0, 0
    End If
End Sub

'====================================================================================
'CLASS Terminate
'====================================================================================
Private Sub Class_Terminate()
    'Unload the WAV file from memory
    If Alias <> "" Then
        'Stop playback
        mciSendString "stop " & Alias, 0&, 0, 0
        'Unload the WAV from memory
        mciSendString "close " & Alias, 0&, 0, 0
        Alias = ""
    End If
End Sub

'=== END ============================================================================
'This work is licenced under the Creative Commons Attribution 2.5 License
